import haxe.remoting.Connection;
import StringTools;

class ServerApiImpl extends haxe.remoting.AsyncProxy<ServerApi> {
}

class Client implements ClientApi {

	var api : ServerApiImpl;
	var name : String;
	static var currRoom : String;
	
	var s : flash.XMLSocket;

	var username : String;
	var verif : String;
	var connected : Bool;
	var lastMsg : Int;

	function new() {
	
		username = flash.Lib.current.username;
		verif = flash.Lib.current.verif;
		connected = false;

		flash.Lib._global.inst = this;
		
	}

	function connect()
	{
		s = new flash.XMLSocket();
		s.onConnect = onConnect;
		//s.connect("82.146.53.245",1024);
		//s.connect("91.185.199.61",8080);
		trace('connecting 1 ');
		//s.connect("localhost", 8082);
		s.connect('82.146.51.53', 8082);
		trace('connecting 2');
		var scnx = haxe.remoting.SocketConnection.socketConnect(s);
		api = new ServerApiImpl(scnx.api);
	}
	
	function onConnect( success : Bool ) 
	{
		if( !success ) 
		{
			trace("Failed to connect on server !");
		}
		else
		{
			trace('onConnect');

			trace('join1' + ' ' + flash.Lib._global.inst.username + ' ' + flash.Lib._global.inst.lastMsg  + '--');
			flash.Lib._global.inst.api.join(flash.Lib._global.inst.username, flash.Lib._global.inst.verif, flash.Lib._global.inst.lastMsg );
			flash.Lib._global.inst.connected = true;
			trace('join2');
		}
	}

	static public function join(lastMsg : Int) 
	{
		flash.Lib._global.inst.lastMsg = lastMsg;
		if(!flash.Lib._global.inst.connected) {
		
			flash.Lib._global.inst.connect();
			return;
		}
	}
	
	static public function leave(lastMsg : Int) {
		if(flash.Lib._global.inst.connected) {
			flash.Lib._global.inst.api.leave(false);
			flash.Lib._global.inst.s.close();
			flash.Lib._global.inst.connected = false;
			return;
		}
	}
	
	static public function say( text : String ) 
	{
		trace(text);
		text = StringTools.ltrim(StringTools.replace(StringTools.replace(text, "\r", ''), "\n", ''));
		trace(text);
		if (flash.Lib._global.inst.connected)
		{
			if (text.substr(0, 1) == "-")
			{
				if (text == "-folks")
				{
					flash.Lib._global.inst.api.whoIsHere(true);
				}
				else if (text == "-all folks")
				{
					flash.Lib._global.inst.api.whoIsHere(false);
				}
				else if (text.substr(0, 5) == "-goto")
				{
					flash.Lib._global.inst.api.gotoRoom(text.substr(6), "", ""); //todo: write in a better way
				}
				else if (text == "-places")
				{
					flash.Lib._global.inst.api.whatPlaces(); 
				}
				else if (text.substr(0, 9) == "-where is")
				{
					flash.Lib._global.inst.api.whereIs(text.substr(10)); //todo: write in a better way
				}
				else if (text == "-where")
				{
					//msg("+sys+", "you are in " + currRoom, 0); //todo-- figure out the msgnum here and in general for system msgs
					var js = haxe.remoting.Connection.jsConnect();
					js.JsMain.systemMsg.call(["you are in " + Client.currRoom]);
				}
			}
			else
			{
				flash.Lib._global.inst.api.say(text);
				trace(text);
			}
		}
	}

	/*
	public function joined( name ) {
		trace('joined');
		var js = haxe.remoting.Connection.jsConnect();
		js.JsMain.joined.call([name]);
	}

	public function left( name ) {
		var js = haxe.remoting.Connection.jsConnect();
		js.JsMain.left.call([name]);
	}*/

	public function msg( from:String, msg:String, msgNum:Int ) : Void
	{
		var js = haxe.remoting.Connection.jsConnect();
		if (from == "+sys+")
		{
				js.JsMain.systemMsg.call([msg, msgNum]);
		}
		else 
		{
			js.JsMain.said.call([from, msg, msgNum]);
		}
	}

	public function missedMsgs( msgs : List<String> ) : Void
	{
		var js = haxe.remoting.Connection.jsConnect();
		js.JsMain.missed.call([msgs]);
	}
	
	public function welcome( msg:String, room:String ) : Void
	{
		currRoom = room;
		var js = haxe.remoting.Connection.jsConnect();
		js.JsMain.welcome.call([msg]);
	}

	public function inRoom( room:String, type:String, url:String ) : Void
	{
		currRoom = room;
	}
	
/*	public function systemMsg( msg : String ) : Void
	{
		var js = haxe.remoting.Connection.jsConnect();
		js.JsMain.systemMsg.call([msg]);
	}
*/
	// --

	static var c : Client;

	static function main() {
		c = new Client();
	}

}
